/*
Author : Nguyen Van Ty
Email  : ngvanty03@gmail.com
Date   : 6/6/2011 

*/

using System;
using System.Collections.Generic;
using System.Text;
using log4net;
using System.Data;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Common;
using BusinessObject;
using Utility;
using System.Xml;
namespace DataAccessLayer
{
    /// <summary>
    /// Define PhieuCapNhatTonKhoDaoBase class. This class user for DataAccessLayer, contain 4 function Insert, Update, Search, Delete.
    /// If you want add new function, you must define in PhieuCapNhatTonKhoDao class , not here.
    /// </summary>
    public class PhieuCapNhatTonKhoDaoBase
    {
        private static ILog logger = null;
        #region Constructor
        /// <summary>
        /// Define Constructor.
        /// </summary>
        public PhieuCapNhatTonKhoDaoBase() { }
        static PhieuCapNhatTonKhoDaoBase()
        {
            //Init logger.
            logger = LoggingUtility.GetLogger(typeof(PhieuCapNhatTonKhoDaoBase).FullName);
        }
        #endregion
        /// <summary>
        /// Read data from IDataReader, convert to PhieuCapNhatTonKho Object.
        /// </summary>
        private static PhieuCapNhatTonKho GetPhieuCapNhatTonKhoByDataReader(IDataReader reader)
        {
            String methodName = "GetPhieuCapNhatTonKhoByDataReader";
            if (logger.IsDebugEnabled)
            {
                LoggingUtility.logMethodEntry(logger, methodName);
            }
            try
            {
                PhieuCapNhatTonKho result = new PhieuCapNhatTonKho();
                //read MaPhieuCapNhatTonKho field data.

                result.MaPhieuCapNhatTonKho = ManagerDao.GetintDataFromDataReader(reader, "MaPhieuCapNhatTonKho");

                //read MaKho field data.

                result.MaKho = ManagerDao.GetintDataFromDataReader(reader, "MaKho");

                //read NgayCapNhat field data.

                result.NgayCapNhat = ManagerDao.GetdatetimeDataFromDataReader(reader, "NgayCapNhat");

                //read NguoiCapNhat field data.

                result.NguoiCapNhat = ManagerDao.GetstringDataFromDataReader(reader, "NguoiCapNhat");

                return result;
            }
            catch (Exception e)
            {
                // write error to log file.
                logger.Error(methodName, e);
                return null;
            }
        }
        /// <summary>
        /// Define function Insert for PhieuCapNhatTonKho Object.
        /// </summary>
        /// <param name="obj">Object Insert.</param>
        /// <returns>
        ///    True: Successfull.
        ///    False: Process Error.
        ///</returns>
        public static int Insert(PhieuCapNhatTonKho obj)
        {
            Int32 id = 0;
            String methodName = "InsertPhieuCapNhatTonKho";
            if (logger.IsDebugEnabled)
            {
                LoggingUtility.logMethodEntry(logger, methodName);
            }
            DbCommand cmd = ManagerDao.GetStoredProcCommand("spInsertPhieuCapNhatTonKho");

            //Add value "MaKho"  paramater.
            ManagerDao.AddInParameter(cmd, "MaKho", DbType.Int32,
                ConvertValueObject.CastObjValueToInsertDB(obj.MaKho));
            //Add value "NgayCapNhat"  paramater.
            ManagerDao.AddInParameter(cmd, "NgayCapNhat", DbType.DateTime,
                ConvertValueObject.CastObjValueToInsertDB(obj.NgayCapNhat));
            //Add value "NguoiCapNhat"  paramater.
            ManagerDao.AddInParameter(cmd, "NguoiCapNhat", DbType.String,
                ConvertValueObject.CastObjValueToInsertDB(obj.NguoiCapNhat));
            //add paramater store return value.        
            ManagerDao.AddParameter(cmd, "returnValue", DbType.Int32, ParameterDirection.ReturnValue, string.Empty, DataRowVersion.Default, null);
            try
            {
                ManagerDao.ExecuteNonQuery(cmd);
                id = Int32.Parse(ManagerDao.GetParameterValue(cmd, "returnValue").ToString());                
            }
            catch (Exception e)
            {
                //write error message to log file.
                logger.Error(methodName, e);
                
            }
            return id;
        }

        /// <summary>
        /// Define function Update for PhieuCapNhatTonKho Object.
        /// </summary>
        /// <param name="obj">Object Update.</param>
        /// <returns>
        ///    True: Successfull.
        ///    False: Process Error.
        ///</returns>
        public static bool Update(PhieuCapNhatTonKho obj)
        {
            bool result = false;
            String methodName = "UpdatePhieuCapNhatTonKho";
            if (logger.IsDebugEnabled)
            {
                LoggingUtility.logMethodEntry(logger, methodName);
            }
            DbCommand cmd = ManagerDao.GetStoredProcCommand("spUpdatePhieuCapNhatTonKho");


            //Add value "MaPhieuCapNhatTonKho"  paramater.
            ManagerDao.AddInParameter(cmd, "MaPhieuCapNhatTonKho", DbType.Int32, ConvertValueObject.CastObjValueToInsertDB(obj.MaPhieuCapNhatTonKho));
            //Add value "MaKho"  paramater.
            ManagerDao.AddInParameter(cmd, "MaKho", DbType.Int32, ConvertValueObject.CastObjValueToInsertDB(obj.MaKho));
            //Add value "NgayCapNhat"  paramater.
            ManagerDao.AddInParameter(cmd, "NgayCapNhat", DbType.DateTime, ConvertValueObject.CastObjValueToInsertDB(obj.NgayCapNhat));
            //Add value "NguoiCapNhat"  paramater.
            ManagerDao.AddInParameter(cmd, "NguoiCapNhat", DbType.String, ConvertValueObject.CastObjValueToInsertDB(obj.NguoiCapNhat));
            // add parameter store return value.
            ManagerDao.AddParameter(cmd, "returnValue", DbType.Int32, ParameterDirection.ReturnValue, string.Empty, DataRowVersion.Default, null);
            try
            {
                ManagerDao.ExecuteNonQuery(cmd);
                int rows = (int)ManagerDao.GetParameterValue(cmd, "returnValue");
                if (rows == 1)//Successfull.
                {
                    result = true;
                }
                else//Error.
                {
                    result = false;
                }

            }
            catch (Exception e)
            {
                //write error Message to log file.
                logger.Error(methodName, e);
                result = false;
            }
            return result;
        }
        /// <summary>
        /// Get Data PhieuCapNhatTonKho object. 
        /// </summary>
        /// <param name="maPhieuCapNhatTonKho" >This is primary key to get data.</param>
        /// <returns>PhieuCapNhatTonKho object.</returns>

        public static PhieuCapNhatTonKho Get(int maPhieuCapNhatTonKho)
        {
            String methodName = "GetPhieuCapNhatTonKho";
            if (logger.IsDebugEnabled)
            {
                LoggingUtility.logMethodEntry(logger, methodName);
            }
            try
            {
                PhieuCapNhatTonKho result = null;
                DbCommand dbCommnand = ManagerDao.GetStoredProcCommand("spGetPhieuCapNhatTonKho");
                //Add value "MaPhieuCapNhatTonKho"  paramater.
                ManagerDao.AddInParameter(dbCommnand, "MaPhieuCapNhatTonKho", DbType.Int32, maPhieuCapNhatTonKho);
                IDataReader reader = ManagerDao.ExecuteReader(dbCommnand);
                if (reader.Read())
                {
                    result = GetPhieuCapNhatTonKhoByDataReader(reader);

                }
                reader.Close();
                return result;
            }
            catch (System.Exception e)
            {
                //write error message to log file.
                logger.Error("Get", e);
                return null;
            }
        }
        /// <summary>
        /// Get All PhieuCapNhatTonKho object from table PhieuCapNhatTonKho.
        /// </summary>
        /// <returns>List PhieuCapNhatTonKho object.</returns>
        public static List<PhieuCapNhatTonKhoResult> GetAll()
        {
            String methodName = "GetAll";
            if (logger.IsDebugEnabled)
            {
                LoggingUtility.logMethodEntry(logger, methodName);
            }

            try
            {
                List<PhieuCapNhatTonKhoResult> listPhieuCapNhatTonKho = new List<PhieuCapNhatTonKhoResult>();
                DbCommand dbCommand = ManagerDao.GetStoredProcCommand("spGetAllPhieuCapNhatTonKho");

                using (IDataReader reader = ManagerDao.ExecuteReader(dbCommand))
                {
                    while (reader.Read())
                    {
                        listPhieuCapNhatTonKho.Add(GetPhieuCapNhatTonKhoResultByDataReader(reader));
                    }
                    reader.Close();
                }

                return listPhieuCapNhatTonKho;
            }
            catch (Exception ex)
            {
                //write error message to log file.
                logger.Error(methodName, ex);
                return null;
            }
        }

        /// <summary>
        /// Search PhieuCapNhatTonKho object in table PhieuCapNhatTonKho with condition is t object.
        /// </summary>
        /// <param name="t">Condition for Search.</param>       
        /// <param name="totalRow">  total records in this table.</param>
        /// <returns>List PhieuCapNhatTonKhoResult object.</returns>
        public static List<PhieuCapNhatTonKhoResult> SearchPhieuCapNhatTonKhoSomeCondition(PhieuCapNhatTonKho t)
        {
            String methodName = "SearchPhieuCapNhatTonKhoSomeCondition";
            if (logger.IsDebugEnabled)
            {
                LoggingUtility.logMethodEntry(logger, methodName);
            }

            try
            {
                List<PhieuCapNhatTonKhoResult> listPhieuCapNhatTonKho = new List<PhieuCapNhatTonKhoResult>();
                DbCommand cmd = ManagerDao.GetStoredProcCommand("spSearchPhieuCapNhatTonKhoByCondition");
                //Add value Condition  paramater.
                ManagerDao.AddInParameter(cmd, "Condition", DbType.String, ConvertValueObject.CastObjValueToInsertDB(t.Condition));

                //Add value PageIndex  paramater.
                ManagerDao.AddInParameter(cmd, "PageIndex", DbType.Int32, t.ObjectPaging.PageIndex);
                //Add value PageSize  paramater.
                ManagerDao.AddInParameter(cmd, "PageSize", DbType.Int32, t.ObjectPaging.PageSize);
                //Add value SortField  paramater.
                ManagerDao.AddInParameter(cmd, "SortField", DbType.String, t.ObjectSortOrder.SortField);
                //Add value SortExpression  paramater.
                ManagerDao.AddInParameter(cmd, "SortExpression", DbType.String, t.ObjectSortOrder.SortExpression);
                using (IDataReader reader = ManagerDao.ExecuteReader(cmd))
                {
                    while (reader.Read())
                    {
                        listPhieuCapNhatTonKho.Add(GetPhieuCapNhatTonKhoResultByDataReader(reader));
                        if (t.ToltalRow <= 0)
                            t.ToltalRow = ManagerDao.Getint32DataFromDataReader(reader, "TotalRow");
                    }

                    reader.Close();
                }

                return listPhieuCapNhatTonKho;
            }
            catch (Exception ex)
            {
                //write error message to log file.
                logger.Error(methodName, ex);
                return null;
            }
        }
        /// <summary>
        /// Delete PhieuCapNhatTonKho object. 
        /// </summary>
        /// <param name="MaPhieuCapNhatTonKho" >This is primary key to delete.</param>
        /// <returns>True/False.</returns>

        public static bool Delete(int MaPhieuCapNhatTonKho)
        {
            String methodName = "DeletePhieuCapNhatTonKho";
            if (logger.IsDebugEnabled)
            {
                LoggingUtility.logMethodEntry(logger, methodName);
            }

            try
            {
                DbCommand dbCommand = ManagerDao.GetStoredProcCommand("spDeletePhieuCapNhatTonKho");
                //Add value "MaPhieuCapNhatTonKho"  paramater.
                ManagerDao.AddInParameter(dbCommand, "MaPhieuCapNhatTonKho", DbType.Int32, MaPhieuCapNhatTonKho);
                //Add value returnValue  paramater.
                ManagerDao.AddParameter(dbCommand, "returnValue", DbType.Int32, ParameterDirection.ReturnValue, string.Empty, DataRowVersion.Default, null);
                try
                {
                    ManagerDao.ExecuteNonQuery(dbCommand);
                    int parameterValue = (int)ManagerDao.GetParameterValue(dbCommand, "returnValue");
                    if (parameterValue == 1)
                    {
                        return true;
                    }
                }
                catch (Exception)
                {
                    throw;
                }
                return false;
            }
            catch (Exception e)
            {
                //write error message to log file.
                logger.Error(methodName, e);
                return false;
            }
        }
        /// <summary>
        /// Read data from IDataReader, convert to PhieuCapNhatTonKhoResult Object.
        /// </summary>
        private static PhieuCapNhatTonKhoResult GetPhieuCapNhatTonKhoResultByDataReaderByLanguage(IDataReader reader, string LanguageId)
        {
            String methodName = "GetPhieuCapNhatTonKhoResultByDataReader";
            if (logger.IsDebugEnabled)
            {
                LoggingUtility.logMethodEntry(logger, methodName);
            }
            try
            {
                PhieuCapNhatTonKhoResult result = new PhieuCapNhatTonKhoResult();
                //read MaPhieuCapNhatTonKho field data.
                result.MaPhieuCapNhatTonKho = ManagerDao.GetintDataFromDataReader(reader, "MaPhieuCapNhatTonKho");

                //read MaKho field data.
                result.MaKho = ManagerDao.GetintDataFromDataReader(reader, "MaKho");

                //read NgayCapNhat field data.
                result.NgayCapNhat = ManagerDao.GetdatetimeDataFromDataReader(reader, "NgayCapNhat");

                //read NguoiCapNhat field data.
                result.NguoiCapNhat = ManagerDao.GetstringDataFromDataReader(reader, "NguoiCapNhat");

                return result;
            }
            catch (Exception e)
            {
                // write error to log file.
                logger.Error(methodName, e);
                return null;
            }
        }
        /// <summary>
        /// Read data from IDataReader, convert to PhieuCapNhatTonKhoResult Object.
        /// </summary>
        private static PhieuCapNhatTonKhoResult GetPhieuCapNhatTonKhoResultByDataReader(IDataReader reader)
        {
            String methodName = "GetPhieuCapNhatTonKhoResultByDataReader";
            if (logger.IsDebugEnabled)
            {
                LoggingUtility.logMethodEntry(logger, methodName);
            }
            try
            {
                PhieuCapNhatTonKhoResult result = new PhieuCapNhatTonKhoResult();
                //read MaPhieuCapNhatTonKho field data.
                result.MaPhieuCapNhatTonKho = ManagerDao.GetintDataFromDataReader(reader, "MaPhieuCapNhatTonKho");

                //read MaKho field data.
                result.MaKho = ManagerDao.GetintDataFromDataReader(reader, "MaKho");

                //read NgayCapNhat field data.
                result.NgayCapNhat = ManagerDao.GetdatetimeDataFromDataReader(reader, "NgayCapNhat");

                //read NguoiCapNhat field data.
                result.NguoiCapNhat = ManagerDao.GetstringDataFromDataReader(reader, "NguoiCapNhat");

                return result;
            }
            catch (Exception e)
            {
                // write error to log file.
                logger.Error(methodName, e);
                return null;
            }
        }

    }
}